<div class="row">
	<div class="col-xs-12">
		<div class="box">
			<div class="box-header">
				<h3 class="box-title">日志管理</h3>
				<div class="box-tools">
					<div class="input-group input-group-sm" style="width: 350px;">
						<input type="text" name="table_search" id="searchNameId"
							class="form-control pull-right" placeholder="用户名">

						<div class="input-group-btn">
							<button type="button" class="btn btn-default btn-search">
                                <i class="fa fa-search"></i>
							</button>
							<button type="button" class="btn btn-default btn-delete">删除</button>
						</div>
					</div>
				</div>
			</div>
			<!-- /.box-header -->
			<div class="box-body table-responsive no-padding">
				<table class="table table-hover">
					<thead>
						<tr>
						    <th><input type="checkbox" id="checkAll">全选</th>
							<th>用户名</th>
							<th>操作</th>
							<th>请求方法</th>
							<th>请求参数</th>
							<th>IP</th>
							<th>执行时长</th>
						</tr>
					</thead>
					<tbody id="tbodyId">
					    <tr>
					       <td colspan="7">数据正在加载中...</td>
					    </tr>
					</tbody>
				</table>
			</div>
			<div id="pageId" class="box-footer clearfix" dm="100">
			  
			</div>
			<!-- /.box-body -->
		</div>
		<!-- /.box -->
	</div>
</div>
<script type="text/javascript">
	$(function (){
		debugger
		$("#pageId").load("doPageUI",function (){
			doGetObjects();
		});
		$(".input-group-btn").on("click",".btn-search",doQueryObjects)
		$(".input-group-btn")
				.on("click",".btn-delete",doDeleteObjects)
		$("#checkAll").on("click",doChangeTBodyCheckBoxState);
	})

	function doGetCheckedIds() {
		var array=[];
		$("#tbodyId input[type=checkbox]").
				each(function (){
					if($(this).prop("checked")){
						array.push($(this).val())
					}
		})
		return array;
	}

	function doChangeTBodyCheckBoxState(){
		debugger
		var flag=$(this).prop("checked");
		$("#tbodyId input[type='checkbox']").prop("checked",flag);
	}

	function doChangeTHeadCheckBoxState(){
		debugger
		var flag=true;
		$("#tbodyId input[type='checkbox']")
		.each(function (){
			flag=flag&$(this).prop("checked")
		});
		$("#checkAll").prop("checked",flag);
	}

	function doRefreshAfterDeleteOK(){
		var pageCount=$("#pageId").data("pageCount");
		var pageCurrent=$("#pageId").data("pageCurrent");
		var checked=$("#checkAll").prop("checked");
		if (pageCurrent==pageCount&&checked&&pageCurrent>1){
			pageCurrent--;
			$("#pageId").data("pageCurrent",pageCurrent);
		}
		doGetObjects();
	}

	function doDeleteObjects() {
		var ids=doGetCheckedIds();
		if (ids.length==0){
			alert("至少选择一个");
			return ;
		}
		var url="log/doDeleteObjects";
		var params={"ids":ids.toString()};
		console.log(params);
		$.post(url,params,function (res){
			if (res.state==1){
				alert(res.message);
				doRefreshAfterDeleteOK();
			}else{
				alert(res.message);
			}
		})
	}

	function doQueryObjects(){
		$("#pageId").data("pageCurrent",1);
		doGetObjects();
	}

	function doGetObjects(){
		debugger
		var url="log/doFindPageObjects";
		var pageCurrent=$("#pageId").data("pageCurrent");
		if(!pageCurrent){pageCurrent=1;}
		var params={"pageCurrent":pageCurrent};
		var username=$("#searchNameId").val();
		if (username){params.username=username;}
		$.getJSON(url,params,function (res){
			doHandleQueryResponseResult(res);
		})
	}

	function doHandleQueryResponseResult(res) {
		debugger
		console.log(res);
		if(res.state==1){
			doSetTableBodyRows(res.data.records);
			doSetPagination(res.data);
			$("#tbodyId input[type=checkbox]").on("click",doChangeTHeadCheckBoxState);
		}else{
			alert(res.message);
		}
	}

	function doSetTableBodyRows(records){
		//清空对象
		var tBody=$("#tbodyId");
		tBody.empty();
		//迭代records并加到tbody
		for (var i in records){
			var tr=$("<tr></tr>")
			var tds=doCreateTds(records[i]);
			tr.append(tds);
			tBody.append(tr);
		}
	}

	function doCreateTds(data){
		var tds="<td><input type='checkbox' class='cBox' name='cItem' value='"+data.id+"'></td>" +
				"<td>"+data.username+"</td>"+
				"<td>"+data.operation+"</td>"+
				"<td>"+data.method+"</td>"+
				"<td>"+data.params+"</td>"+
				"<td>"+data.ip+"</td>"+
				"<td>"+data.time+"</td>";
		return tds;
	}


</script>






